package com.wzdigit.wms.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.framework.Shift;
import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.domain.mid.IfTransferDtl;
import com.wzdigit.wms.basic.domain.mid.IfTransferHeader;
import com.wzdigit.wms.basic.domain.mid.dtos.IfTransferHeaderDto;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.basic.mapper.core.mid.IfTransferDtlMapper;
import com.wzdigit.wms.basic.mapper.core.mid.IfTransferHeaderMapper;
import com.wzdigit.wms.wms.service.mid.IfTransferHeaderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import net.dreamlu.mica.core.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "单据手动同步功能")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class SyncTestController {

    private final String REQUEST_MODEL = "/Test0414";
    @Autowired
    private IfTransferHeaderService ifTransferHeaderService;
    @Autowired
    private IfTransferHeaderMapper ifTransferHeaderMapper;
    @Autowired
    private IfTransferDtlMapper ifTransferDtlMapper;

    /**
     * @param billNo
     * @param servletRequest
     * @param servletResponse
     */
    @ApiOperation(value = "单据手动同步功能", notes = "单据手动同步功能")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/SyncBillByNo", method = RequestMethod.GET)
    public void scanSn(String billNo,
                       HttpServletRequest servletRequest,
                       HttpServletResponse servletResponse) {
        try {
            QueryWrapper<IfTransferHeader> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("BILL_NO", billNo);
            List<IfTransferHeader> ifTransferHeaders = this.ifTransferHeaderMapper.selectList(queryWrapper);
            if (ifTransferHeaders.size() == 0)
                return;
            IfTransferHeader ifTransferHeader = ifTransferHeaders.get(0);

            IfTransferHeaderDto ifTransferHeaderDto = new IfTransferHeaderDto();
            BeanUtil.copyNonNull(ifTransferHeader, ifTransferHeaderDto);
            List<IfTransferDtl> ifTransferDtls = ifTransferDtlMapper.selectList(
                    new QueryWrapper<>(
                            new IfTransferDtl().setIfTransferHeaderId(ifTransferHeader.getIfTransferHeaderId())
                    )
            );
            ifTransferHeaderDto.setIfTransferDtlList(ifTransferDtls);

            boolean isSyncError = false;
            try {
                ifTransferHeaderService.syncData(ifTransferHeaderDto, isSyncError);
            } catch (Exception e) {
                Shift.fatal(e.getMessage());
                //e.printStackTrace();
            }

        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

}
